<template>
  <div>
    <!-- 顶部单选框 -->
    <div>
      未上架<input
        type="radio"
        name="aa"
        @click="ShowStorageInNO"
        checked="checked"
      />
      已上架<input type="radio" name="aa" @click="ShowStorageInYES" />
    </div>
    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search ||
            data.medicine.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="药品名字" prop="medicine.name"> </el-table-column>
      <el-table-column label="入库数量" prop="inNum"> </el-table-column>
      <el-table-column label="入库时间" prop="inTime"> </el-table-column>
      <el-table-column label="生产日期" prop="dateManufacture">
      </el-table-column>
      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button
            v-if="look"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >上架</el-button
          >
          <!-- <el-button
            v-if="look"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >药品异常</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <br /><br />
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[8, 10, 12]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  //初始化数据
  created() {
    this.$axios
      .get(
        "http://localhost:8080/medicines/showAllStorageIn/" +
          this.currentPage +
          "/" +
          this.page,
      )
      .then((src) => {
        this.tableData = src.data.data.list;
        this.pageInfo = src.data.data;
      });
  },
  data() {
    return {
      tableData: [],
      search: "",
      look: true,
      currentPage: 1,
      page: 8,
      pageInfo: {},
    };
  },
  methods: {
    handleSizeChange(val) {
      this.page = val;
      //根据look的状态判断查询的是那个
      if (this.look) {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllStorageIn/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      } else {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllStorageInYes/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.look) {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllStorageIn/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      } else {
        this.$axios
          .get(
            "http://localhost:8080/medicines/showAllStorageInYes/" +
              this.currentPage +
              "/" +
              this.page
          )
          .then((src) => {
            this.tableData = src.data.data.list;
            this.pageInfo = src.data.data;
          });
      }
    },
    ShowStorageInYES() {
      this.look = false;
      this.$axios
        .get(
          "http://localhost:8080/medicines/showAllStorageInYes/" +
            this.currentPage +
            "/" +
            this.page
        )
        .then((src) => {
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    ShowStorageInNO() {
      this.look = true;
      this.$axios
        .get(
          "http://localhost:8080/medicines/showAllStorageIn/" +
            this.currentPage +
            "/" +
            this.page
        )
        .then((src) => {
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    handleEdit(index, row) {
      this.$axios
        .post("http://localhost:8080/medicines/addPharmacyStorage", {
          medicineId: row.medicine.id,
          name: row.medicine.name,
          cont: row.inNum,
          deadline: row.medicine.deadline,
          category: row.medicine.category,
          price: row.medicine.price,
          brand: row.medicine.brand,
          dateManufacture: row.dateManufacture,
          Headers:{
            "token":JSON.parse(localStorage.getItem("user")).token
          }
        })
        .then((src) => {
          if (src.data.code == 200) {
                this.$message({ message: src.data.msg, type: "success" });
              } else {
                this.$message.error(src.data.msg);
              }
          this.ShowStorageInNO();
        });
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
};
</script>

<style lang="less" scoped>
.show {
  margin: 20px;
}
.header {
  display: flex;
  button {
    margin-left: 20px;
  }
}
.wrapper {
  margin: 20px;
}
</style>
